package com.yaorange.web.controller.nongjt;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.yaorange.common.utils.StringUtils;
import com.yaorange.nongjt.domain.NjtUserPerfect;
import com.yaorange.nongjt.vo.NjtCertificateVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yaorange.common.annotation.Log;
import com.yaorange.common.core.controller.BaseController;
import com.yaorange.common.core.domain.AjaxResult;
import com.yaorange.common.enums.BusinessType;
import com.yaorange.nongjt.domain.NjtCertificate;
import com.yaorange.nongjt.service.INjtCertificateService;
import com.yaorange.common.utils.poi.ExcelUtil;
import com.yaorange.common.core.page.TableDataInfo;

/**
 * 合格证打印数据Controller
 * 
 * @author juheng
 * @date 2025-02-28
 */
@RestController
@RequestMapping("/nongjt/certificate")
public class NjtCertificateController extends BaseController
{
    @Autowired
    private INjtCertificateService njtCertificateService;

    /**
     * 查询合格证打印数据列表
     */
    @PreAuthorize("@ss.hasPermi('nongjt:certificate:list')")
    @GetMapping("/list")
    public TableDataInfo list(NjtCertificateVo njtCertificate)
    {
        // 转换userPerfectString为userPerfect对象
        if (StringUtils.isNotEmpty(njtCertificate.getUserPerfectString())) {
            njtCertificate.setUserPerfect(JSON.parseObject(njtCertificate.getUserPerfectString(), NjtUserPerfect.class));
        }
        List<NjtCertificateVo> list = njtCertificateService.selectNjtCertificateList(njtCertificate);
        return getDataTable(list);
    }

    /**
     * 导出合格证打印数据列表
     */
    @PreAuthorize("@ss.hasPermi('nongjt:certificate:export')")
    @Log(title = "合格证打印数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NjtCertificateVo njtCertificate)
    {
        List<NjtCertificateVo> list = njtCertificateService.selectNjtCertificateList(njtCertificate);
        ExcelUtil<NjtCertificateVo> util = new ExcelUtil<NjtCertificateVo>(NjtCertificateVo.class);
        util.exportExcel(response, list, "合格证打印数据数据");
    }

    /**
     * 获取合格证打印数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('nongjt:certificate:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(njtCertificateService.selectNjtCertificateById(id));
    }

    /**
     * 新增合格证打印数据
     */
    @PreAuthorize("@ss.hasPermi('nongjt:certificate:add')")
    @Log(title = "合格证打印数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NjtCertificate njtCertificate)
    {
        return toAjax(njtCertificateService.insertNjtCertificate(njtCertificate));
    }

    /**
     * 修改合格证打印数据
     */
    @PreAuthorize("@ss.hasPermi('nongjt:certificate:edit')")
    @Log(title = "合格证打印数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NjtCertificate njtCertificate)
    {
        return toAjax(njtCertificateService.updateNjtCertificate(njtCertificate));
    }

    /**
     * 删除合格证打印数据
     */
    @PreAuthorize("@ss.hasPermi('nongjt:certificate:remove')")
    @Log(title = "合格证打印数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(njtCertificateService.deleteNjtCertificateByIds(ids));
    }
}
